package com.imooc.ioc.demo1.Control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.imooc.ioc.demo1.Service.UserService;
import com.imooc.ioc.demo1.common.JsonUtil;
import com.imooc.ioc.demo1.common.Status;
import com.imooc.ioc.demo1.vo.Register;
import com.imooc.ioc.demo1.vo.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Controller
public class User {

    @Autowired
    UserService userService ;

    @PostMapping("/login")
    @ResponseBody
    public String login( @RequestBody  com.imooc.ioc.demo1.vo.User user , HttpServletRequest request) throws IOException {

        System.out.println(user);

        if(user.getVercode() == null || user.getVercode().equals("")){
            return  new JsonUtil<String>().generator(Status.ERROR.getCode(),"验证码错误");
        }

        // 进行验证码的判断，验证码会区分大小写
        // 验证成功后删除验证码
        // 或者给验证码加上过期时间
        HttpSession session =  request.getSession();

        System.out.println(session.getAttribute("vercode"));

        if(session.getAttribute("vercode") == null){

            return  new JsonUtil<String>().generator(Status.ERROR.getCode(),"验证码错误");
        }

        if(user.getVercode().equalsIgnoreCase((String) session.getAttribute("vercode") ) ){

            // 登陆成功后，将用户的信息存入session
            if( userService.select(user)){

                session.setAttribute("user",userService.select(user.getUsername()));

              return new JsonUtil<String>().generator(Status.SUCCESS.getCode(),"登陆成功");

            }else {

               return   new JsonUtil<String>().generator(Status.ERROR.getCode(),"账号密码错误");
            }

        }

            return  new JsonUtil<String>().generator(Status.ERROR.getCode(),"验证码错误");

    }

    @PostMapping("/register")
    @ResponseBody
    public String   register(@RequestBody Register register , HttpServletRequest request ) throws JsonProcessingException, ParseException {

        System.out.println(register);

        HttpSession session =  request.getSession();

          Date date = new Date();//获得系统时间.
          SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
          String nowTime = sdf.format(date);

          register.setDate(nowTime);


          // 判断用户信息是否已存在的操作已经在 service 实现
          // 如果用户信息插入成功，则把用户信息存入 session ， 然后通知前端用户登陆成功
          if(userService.insert(register)){

              session.setAttribute("user",register);

              return new JsonUtil<String>().generator(Status.SUCCESS.getCode(),"注册成功");

          }else {

              return new JsonUtil<String>().generator(Status.ERROR.getCode(),"注册失败");
          }


    }

    private Register VerifyLogin(HttpServletRequest request){

        HttpSession session =  request.getSession();

        Register register = (Register) session.getAttribute("user");

       return register;

    }


    @GetMapping("/userInfo")
    @ResponseBody
    public String userInfo(HttpServletRequest request) throws JsonProcessingException {

      Register register =  VerifyLogin(request);
        if(register == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请先登陆注册");
        }

        return new JsonUtil<Register>().generator(Status.SUCCESS.getCode(),register);
    }

    // 可以拿到前端传来的分页的参数
    @GetMapping("/getAllUser")
    @ResponseBody
    public String getAllUser(HttpServletRequest request , Integer page , Integer limit) throws JsonProcessingException  {

        System.out.println(page);

        System.out.println(limit);

        Register register =  VerifyLogin(request);

        if(register == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请先登陆注册");
        }

       ArrayList<Register>  allUser = userService.getAllUser(page,limit);

        Integer total = userService.getTotalUser();


        for(Register i : allUser){

            System.out.println(allUser);
        }

        UserInfo userInfo = new UserInfo();

        userInfo.setTotal(total);

        userInfo.setUserInfoAll(allUser);

        return new JsonUtil<UserInfo>().generator(Status.SUCCESS.getCode(),userInfo);

    }

    @PostMapping("/updateUser")
    @ResponseBody
    public String updateUser(@RequestBody Register userInfo , HttpServletRequest request) throws JsonProcessingException {

        // 判断用户登陆了没，没登陆，不允许修改用户的信息表

        Register register =  VerifyLogin(request);
        if(register == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请先登陆注册");
        }


        //判断数据是否有问题在 control 层 ，判断 用户是否已经被使用也在  control  层

        // 进行更新信息的验证判断
        System.out.println(userInfo);

        if(!(userInfo.getRoles().equals("用户") || userInfo.getRoles().equals("管理员") || userInfo.getRoles().equals("超级管理员"))){

            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"输入的角色信息有误");

        }

        if(userInfo.getPassword().equals("") || userInfo.getPassword() == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请输入密码");
        }

        if(userInfo.getUsername().equals("") || userInfo.getUsername() == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请输入账号信息");
        }

        if(userService.selectUser(userInfo.getUsername(), userInfo.getId())){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"账号信息已存在，请重新输入");
        }


        userService.updateUser(userInfo);

        return new JsonUtil<String>().generator(Status.SUCCESS.getCode(),"更新成功");

    }

    @PostMapping("/del")
    @ResponseBody
    public String del(@RequestBody Map<String , Integer>  map , HttpServletRequest request) throws JsonProcessingException {

        // 判断用户登陆了没，没登陆，不允许删除用户的信息表
        Register register =  VerifyLogin(request);
        if(register == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请先登陆注册");
        }


        System.out.println(map.get("id"));

       userService.delete(map.get("id"));

        return new JsonUtil<String>().generator(Status.SUCCESS.getCode(),"删除成功");
    }


    @GetMapping("/dropOut")
    public String dropOut( HttpServletRequest request ) throws JsonProcessingException {

        Register register =  VerifyLogin(request);
        if(register == null){
            return new JsonUtil<String>().generator(Status.ERROR.getCode(),"请先登陆注册");
        }

        request.getSession().removeAttribute("user");

        return new JsonUtil<String>().generator(Status.SUCCESS.getCode(),"退出成功");

    }




}
